package com.ews.onlineexchange.controller.assess;

import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import com.ews.onlineexchange.model.assess.AssessClass;
import com.ews.onlineexchange.service.EntityHome;
import com.ews.onlineexchange.service.assess.AssessClassService;

@Named
@ConversationScoped
public class AssessClassHome extends EntityHome<AssessClass, String> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Inject
	AssessClassService assessClassService;
	private String tempName;
	
	@Override
	public AssessClass loadInstance() {
		AssessClass instance = super.loadInstance();
		setTempName(instance.getName());
		return instance;
	}


	@Override
	public String saveAndFlush() {
		String currName = getInstance().getName().trim();
		if(!currName.equals(tempName)) {//新增时判断
			if(assessClassService.findByName(currName)) {
				FacesContext.getCurrentInstance().addMessage("name",
						new FacesMessage(FacesMessage.SEVERITY_ERROR, "评价类别已存在！", ""));
				return null;
			}
		}
		String result = super.saveAndFlush();
		if(result.equals(RESULT_SAVED))
			return "AssessClassList.xhtml?faces-redirect=true&amp;includeViewParams=true";
		else return null;
	}


	public String getTempName() {
		return tempName;
	}


	public void setTempName(String tempName) {
		this.tempName = tempName;
	}
	
	
}
